package com.bw.exam.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.exam.pojo.Specification;
import com.bw.exam.pojo.SpecificationOption;
import com.bw.exam.pojo.TypeTemplate;
import com.bw.exam.result.R;
import com.bw.exam.service.TypeTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liuzhiping
 * @date: 2024/7/18 15:06
 **/
@Slf4j
@RestController
@RequestMapping("/manage/typeTemplate")
public class TypeTemplateController {


    @Resource
    TypeTemplateService typeTemplateService;

    /**
     * 列表数据展示方法
     * @param typeTemplate
     * @return
     */
    @RequestMapping("list")
    public R list(@RequestBody TypeTemplate typeTemplate){

        log.info("typeTemplate:{}", JSON.toJSONString(typeTemplate));

        LambdaQueryWrapper<TypeTemplate> queryWarrper = new LambdaQueryWrapper<TypeTemplate>();
        if(StringUtils.isNotBlank(typeTemplate.getName())){
            queryWarrper.like(TypeTemplate::getName,typeTemplate.getName());
        }

        Page<TypeTemplate> pagedb = new Page<TypeTemplate>(typeTemplate.getStartPage(),typeTemplate.getEndPage());

        Page<TypeTemplate> pageResult = this.typeTemplateService.page(pagedb, queryWarrper);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",pageResult.getRecords());
        map.put("total",pageResult.getTotal());

        return R.success(map);
    }


    /**
     * 列表数据展示方法
     * @return
     */
    @RequestMapping("listAll")
    public R listAll(){
        return R.success(this.typeTemplateService.list());

    }


    @RequestMapping("add")
    public R add(@RequestBody TypeTemplate typeTemplate){
        return R.success(this.typeTemplateService.save(typeTemplate));
    }


    //查询接口
    @RequestMapping("/findById/{id}")
    public R findById(@PathVariable Long id){
        log.info("typeTemplate 查询 typeTemplate:{}",id);
        TypeTemplate typeTemplate = this.typeTemplateService.getById(id);
        return R.success(typeTemplate);
    }

    @RequestMapping("saveOrUpdate")
    public R saveOrUpdate(@RequestBody TypeTemplate typeTemplate){
        return R.success(this.typeTemplateService.saveOrUpdate(typeTemplate));
    }

}

